2023-03-06

Die YouTube API

Vorteile der YouTube API

Vorteile gegenüber Webscraping

  • sauber formatierte, verlässliche Daten
  • legale, durch Terms of Service regulierte Datenbeschaffung
  • gute Dokumentation
  • keine HTML-Kenntnisse nötig
  • R-Packages zum Umgang mit der API, z.B. tuber

Nachteile

  • Mögliche Daten durch die Endpoints der API vorgegeben
  • Rate Limits reglementieren die täglich verfügbare Datenmenge
  • Authentifizierung via OAuth 2.0 / API Keys etwas umständlich

Fallbeispiel: Youtube Kanäle der Parteien

Die YouTube API mit R

Zugriff auf die YouTube API

devtools::install_github("soodoku/tuber", build_vignettes = TRUE) # the latest development version is required to get related videos, due to a bug fix

library(tuber)
library(tidyverse)

yt_oauth("81481321824-btqvdmev607hbbq3n34i4qpb194spdq7.apps.googleusercontent.com",
         "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")

YouTube Kanäle der Parteien

party_channels <- tibble(
  party = c("cdu",
            "spd",
            "gruene",
            "fdp", 
            "afd", 
            "linke"),
  channel_id = c("UCKyWIEse3u7ExKfAWuDMVnw",
                 "UCSmbK1WtpYn2sOGLvSSXkKw",
                 "UC7TAA2WYlPfb6eDJCeX4u0w",
                 "UC-sMkrfoQDH-xzMxPNckGFw",
                 "UCq2rogaxLtQFrYG3X3KYNww",
                 "UCA95T5bSGxNOAODBdbR2rYQ")
)

Zugriff auf die Kanäle

party_videos <- tibble() # empty container object to fill

for (i in 1:nrow(party_channels)) { # loop through every row in the dataframe of party channels
  
  videos <-  list_channel_videos(channel_id = party_channels$channel_id[i], # make the API call
                                 max_results = Inf)
  
  videos <- videos %>% mutate(party = party_channels$party[i]) # add party indicator

  party_videos <- bind_rows(party_videos, videos) # bind results
  
  cat(party_channels$party[i], ": ", nrow(videos), " videos retrieved \n", sep = "") # some printout to keep track
  
  Sys.sleep(1) # a sleep period between calls to avoid API errors
  
}
# A tibble: 13,512 × 7
   kind                 etag     id    conte…¹ contentDetails.vi…² party party…³
   <chr>                <chr>    <chr> <chr>   <dttm>              <chr> <chr>  
 1 youtube#playlistItem ZFCvDiu… VVVL… FrUl-L… 2023-02-13 13:11:50 cdu   CDU    
 2 youtube#playlistItem VXoWAF5… VVVL… HwIrFN… 2023-02-09 08:51:54 cdu   CDU    
 3 youtube#playlistItem -ftUwhz… VVVL… ECbwvC… 2023-02-06 15:34:06 cdu   CDU    
 4 youtube#playlistItem sGsp6u9… VVVL… GEWooG… 2023-02-06 07:56:57 cdu   CDU    
 5 youtube#playlistItem 8hQr8EX… VVVL… tteB0E… 2023-01-31 12:55:49 cdu   CDU    
 6 youtube#playlistItem 1RgV2FH… VVVL… 2SZ1Uy… 2023-01-31 11:59:15 cdu   CDU    
 7 youtube#playlistItem heuBQn3… VVVL… mddH32… 2023-01-27 08:30:36 cdu   CDU    
 8 youtube#playlistItem 0ru_bdz… VVVL… 7CCa3Q… 2023-01-24 15:50:02 cdu   CDU    
 9 youtube#playlistItem LyH77F7… VVVL… vDYyk3… 2023-01-20 16:28:18 cdu   CDU    
10 youtube#playlistItem zlMPbUn… VVVL… as1_qh… 2023-01-20 13:07:11 cdu   CDU    
# … with 13,502 more rows, and abbreviated variable names
#   ¹​contentDetails.videoId, ²​contentDetails.videoPublishedAt, ³​party_label